package com.acce.gxsc.page.reportForm;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.acce.gxsc.R;
import com.acce.gxsc.model.netBean.ReportFormBean;
import com.acce.gxsc.page.reportForm.goodsForm.GoodsReportFormFragment;
import com.acce.sdk.BaseActivity;
import com.acce.sdk.base.mvp.BaseMvpFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by huxley on 2017/5/26.
 */
public class ReportFormFragment extends BaseMvpFragment<ReportFormView, ReportFormPresenter> implements ReportFormView {

    LayoutInflater mInflater;

    View rootView;
    @Bind(R.id.tabs)
    TabLayout mTabs;
    @Bind(R.id.viewpager)
    ViewPager mViewpager;

    public static ReportFormFragment newInstance() {
        Bundle args = new Bundle();
        ReportFormFragment fragment = new ReportFormFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected ReportFormPresenter createPresenter() {
        return new ReportFormPresenter();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_report_form, container, false);
        ButterKnife.bind(this, rootView);
        init();
        setHasOptionsMenu(true);
        mPresenter.loadData();
        return rootView;
    }

    Adapter adapter;
    private void init() {
        mInflater = LayoutInflater.from(getActivity());
        adapter = new Adapter(getChildFragmentManager());
        GoodsReportFormFragment goodsReportFormFragment = GoodsReportFormFragment.newInstance("按商品类别");
        adapter.addFragment(goodsReportFormFragment, "按商品类别查询");
        GoodsReportFormFragment companyReportFormFragment = GoodsReportFormFragment.newInstance("按报备公司");
        adapter.addFragment(companyReportFormFragment, "按报备公司查询");
        GoodsReportFormFragment ownerReportFormFragment = GoodsReportFormFragment.newInstance("按货主");
        adapter.addFragment(ownerReportFormFragment, "按货主查询");
        mViewpager.setAdapter(adapter);
        mTabs.setupWithViewPager(mViewpager);
        mViewpager.setOffscreenPageLimit(adapter.getCount());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void showProgressDialog(String s, boolean b) {
        ((BaseActivity) getActivity()).showProgressDialog(s, b);
    }

    @Override
    public void hideProgressDialog() {
        ((BaseActivity) getActivity()).hideProgressDialog();
    }

    @Override
    public void printSuccess() {

    }

    @Override
    public void loadSuccess(ArrayList<ArrayList<ReportFormBean>> datas) {
        adapter.getFragment(0).load(datas.get(0));
        adapter.getFragment(1).load(datas.get(1));
        adapter.getFragment(2).load(datas.get(2));
    }


    private static class Adapter extends FragmentPagerAdapter {
        private final List<BaseMvpFragment> mFragments      = new ArrayList<>();
        private final List<String>          mFragmentTitles = new ArrayList<>();

        private Adapter(FragmentManager fm) {
            super(fm);
        }

        private void addFragment(BaseMvpFragment fragment, String title) {
            mFragments.add(fragment);
            mFragmentTitles.add(title);
        }

        public GoodsReportFormFragment getFragment(int position) {
            return (GoodsReportFormFragment) mFragments.get(position);
        }

        @Override
        public BaseMvpFragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitles.get(position);
        }
    }
}
